#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using FreeOnlineApplication.Class.Repository;
using FreeOnlineApplication.Class.Entity;
using System.Collections;

#endregion

namespace FreeOnlineApplication
{
    public partial class BookingSearchForm : Form
    {
        BookingRepository m_rep; 
        CustomerRepository m_cusRep; 
        RoomMasterRepository m_rmRep;
        Booking m_subject = new Booking();
        IMainForm m_mainForm;
        IMasterSearchBooking m_parent;

        public BookingSearchForm(IMainForm mainform, IMasterSearchBooking parent)
        {
            InitializeComponent();
            m_rep = new BookingRepository(mainform.Connection);
            m_cusRep = new CustomerRepository(mainform.Connection);
            m_rmRep = new RoomMasterRepository(mainform.Connection);
            m_mainForm = mainform;
            m_parent = parent;
            loadRecords();
        }
        private void loadRecords()
        {
            listView1.Items.Clear();
            IList records = m_rep.GetAllOpen(m_mainForm.CurrentUnit.ID);
            foreach (Booking d in records)
            {
                d.CUSTOMER = m_cusRep.GetById(d.CUSTOMER.ID);
                d.ROOM_MASTER = m_rmRep.GetById(d.ROOM_MASTER.ID);
                ListViewItem item = listView1.Items.Add(d.CODE);
                item.SubItems.Add(d.CUSTOMER.ID_NO);
                item.SubItems.Add(d.CUSTOMER.NAME);
                item.SubItems.Add(d.ROOM_MASTER.CODE);
                item.SubItems.Add(d.START_DATE_TIME.ToString("dd-MM-yyyy HH:mm"));
                item.SubItems.Add(d.END_DATE_TIME.ToString("dd-MM-yyyy HH:mm"));
                item.SubItems.Add(d.STATUS);
                item.Tag = d;
            }
        }
        private void buttonFind_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            IList records = m_rep.GetAllSearchOpen(textBox1.Text.Trim(), m_mainForm.CurrentUnit.ID);
            if (records.Count == 0)
            {
                ListViewItem item = listView1.Items.Add("Data tidak di temukan");
            }
            foreach (Booking d in records)
            {
                d.CUSTOMER = m_cusRep.GetById(d.CUSTOMER.ID);
                d.ROOM_MASTER = m_rmRep.GetById(d.ROOM_MASTER.ID);
                ListViewItem item = listView1.Items.Add(d.CODE);
                item.SubItems.Add(d.CUSTOMER.ID_NO);
                item.SubItems.Add(d.CUSTOMER.NAME);
                item.SubItems.Add(d.ROOM_MASTER.CODE);
                item.SubItems.Add(d.START_DATE_TIME.ToString("dd-MM-yyyy HH:mm"));
                item.SubItems.Add(d.END_DATE_TIME.ToString("dd-MM-yyyy HH:mm"));
                item.SubItems.Add(d.STATUS);
                item.Tag = d;
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItem == null) return;
            m_subject = (Booking)listView1.SelectedItem.Tag;
            m_parent.LoadByBooking(m_subject);
            this.Close();
        }
    }
}